//格式化
export const timeFormat = (time,type = 'date',delimiter = '-') => {
  const timestamp = time ? new Date(time): new Date();
  const year = timestamp.getFullYear();
  const month = (timestamp.getMonth() + 1) > 9 ? timestamp.getMonth() + 1 : '0' + (timestamp.getMonth() + 1);
  const day = timestamp.getDate() > 9 ? timestamp.getDate() : '0' + timestamp.getDate();

  const hour = timestamp.getHours() > 9 ? timestamp.getHours() : '0' + timestamp.getHours();
  const minute = timestamp.getMinutes() > 9 ? timestamp.getMinutes() : '0' + timestamp.getMinutes();
  const second = timestamp.getSeconds() > 9 ? timestamp.getSeconds() : '0' + timestamp.getSeconds();

  if(type === 'date') {
    return `${year}${delimiter}${month}${delimiter}${day}`
  }
  if(type === 'time') {
    return `${hour}:${minute}:${second}`
  }
  if(type === 'datetime') {
    return `${year}${delimiter}${month}${delimiter}${day} ${hour}:${minute}:${second}`
  }
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

//早于
export const timeThan = (time1 : string,time2: string,type : 'less' | 'great' = 'less') : boolean => {
  const time1Stamp = time1 ? new Date(time1).getTime() : new Date().getTime()
  const time2Stamp = time2 ? new Date(time2).getTime() : new Date().getTime()
  return type === 'less' ? (time1Stamp <= time2Stamp) : (time1Stamp >= time2Stamp)
}
